% (1) Load the Ground Truth in corresponding posture (vertices, triangles, vSignature, distanceMtr)
% Do it manually before running the script. For example, if you have computed
% matching from centaur0 to centaur1, you need to load centaur1 data for
% this script.
% (2) Load output matching data of our method

% Add Tools to Path
addpath('../../Tools');

% Geometry and faces
nV = size(vertices, 1);
nT = size(triangles, 1);

% Number of samples that have been matched
nSamples = size(MAP, 1);
% vertex index - to - triangle index
vi2tri = MAP(:, 1:2);

% Baricentric coordinates of vi2tri matching
baricentric = MAP(:, 3:5);

% Compare our method matchings to Ground Truth
Err = zeros(nSamples, 1);
for i = 1 : nSamples
    vi = vi2tri(i, 1);
    
    groundTruth = vertices(vi, :)';
    
    % Corresponding triangle 
    tri = triangles( vi2tri(i, 2), : );
    V = zeros(3);
    V = vertices(tri, :)';
    bar = baricentric(i, :)';
    ourMethodCorr = V * bar;
    
    % Compute Euclidean norm / Geodesic could be an option
    Err(i) = norm( groundTruth - ourMethodCorr );
end % for

AveErr = mean(Err)
MaxErr = max(Err)

% Normalize error values by square root of mesh area
meshArea = triMeshArea(vertices, triangles);
AveErr = AveErr / sqrt(meshArea)
MaxErr = MaxErr / sqrt(meshArea)